﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Table=FCNS.Data.Table;

namespace POS.StoreTemplete
{
    /// <summary>
    /// WindowProduct.xaml 的交互逻辑
    /// </summary>
    public partial class SelectProduct : Window
    {
        public SelectProduct()
        {
            InitializeComponent();

            InitVar();
            InitEvent();
        }

        Table.ProductS ps = null;
        /// <summary>
        /// 已选择的商品
        /// </summary>
        public Table.ProductS Product
        {
            get { return ps; }
        }

        public System.Collections.IEnumerable ItemsSource
        {
            get { return dataGridObject.ItemsSource; }
            set
            {
                dataGridObject.ItemsSource = value;
                if (!dataGridObject.HasItems)
                    return;

                    dataGridObject.SelectedIndex = 0;
                    this.Height = 75 + dataGridObject.Items.Count * 35;
            }
        }

        private void InitVar()
        {
            dataGridObject.ShowBodyMenu = false;
            dataGridObject.ShowHeaderMenu = false;
        }
    
        private void InitEvent()
        {
            this.KeyDown += (ss, ee) => { KeyDowned(ee.Key); };
            dataGridObject.MouseDoubleClick += (ss, ee) => { GetProduct(); };
            dataGridObject.KeyDown += (ss, ee) => { KeyDowned(ee.Key); };
        }

        private void KeyDowned(Key key)
        {
            switch (key)
            {
                case Key.Enter:
                    GetProduct();
                    break;

                case Key.Down:
                    if (dataGridObject.SelectedIndex + 1 < dataGridObject.Items.Count)
                        dataGridObject.SelectedIndex += 1;
                    break;

                case Key.Up:
                    if (dataGridObject.SelectedIndex > 0)
                        dataGridObject.SelectedIndex -= 1;
                    break;

                case Key.Escape:
                    this.Close();
                    break;
            }
        }

        private void GetProduct()
        {
            if (dataGridObject.SelectedItem == null)
                return;

            ps = (Table.ProductS)dataGridObject.SelectedItem;
            this.Close();
        }
    }
}
